using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Rss;
using NTTHAO.Common;
using NTTHAO.Web.Rss;
using System.Globalization;

public partial class admin_webparts_Plug_ins_RssNews_RssNews : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + "&g=" + Request.QueryString["g"] + (Request.QueryString["m"] == null ? "" : ("&m=" + Request.QueryString["m"]));
            string g = Request.QueryString["g"];
            string action = Request.QueryString["action"];
            string controlName = string.Empty;

            if (g != null)
            {
                if ("removenews".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    int menuID = 0, newsID = 0;
                    Int32.TryParse(Request.QueryString["m"], out menuID);
                    Int32.TryParse(Request.QueryString["id"], out newsID);

                    if (menuID > 0 && newsID > 0)
                    {
                        RssContentInMenu.RemoveNews(menuID, newsID);
                    }

                    string returnUrl = bUrl;
                    if (Request.QueryString["ReturnUrl"] != null)
                    {
                        returnUrl = Request.QueryString["ReturnUrl"];
                    }
                    Response.Redirect(returnUrl, false);
                    return;
                }
                else if ("deletenews".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    int menuID = 0, newsID = 0;
                    Int32.TryParse(Request.QueryString["m"], out menuID);
                    Int32.TryParse(Request.QueryString["id"], out newsID);

                    if (menuID > 0 && newsID > 0)
                    {
                        RssContentInMenu cim = new RssContentInMenu(menuID, newsID);
                        cim.Activated = false;
                        cim.Update();
                    }

                    string returnUrl = bUrl;
                    if (Request.QueryString["ReturnUrl"] != null)
                    {
                        returnUrl = Request.QueryString["ReturnUrl"];
                    }
                    Response.Redirect(returnUrl, false);
                    return;
                }
                else if ("updatenews".Equals(action, StringComparison.OrdinalIgnoreCase))
                {
                    int groupID = 0, menuID = -1;
                    Int32.TryParse(Request.QueryString["g"], out groupID);
                    Int32.TryParse(Request.QueryString["m"], out menuID);

                    if (groupID > 0 && menuID > -1)
                    {
                        NTTHAO.CommonWeb.GlobalUpdateRssNews.UpdateNews(groupID, menuID);
                    }

                    //Response.Redirect(bUrl, false);
                    return;
                }
                else
                {
                    controlName = "RssNewsList.ascx";
                }
            }

            if (controlName == string.Empty)
            {
                //controlName = "RssContentTest.ascx";
                controlName = "RssGroupList.ascx";
            }

            Control ctrl = this.LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            this.Controls.Add(ctrl);
        }
    }

    /*
    public void UpdateNews(int groupID, int menuID)
    {
        try
        {
            RssGroupTree tree = RssPageGroup.GetTree(groupID);
            if (tree == null) return;

            RssMenuNode node = tree.Node;
            while (node != null && node.Menu.ID != menuID)
            {
                if (node.Children != null)
                {
                    node = node.Children[0];
                }
                else if (node.NextNode != null)
                {
                    node = node.NextNode;
                }
                else
                {
                    bool next = false;
                    while (!next && node != null)
                    {
                        node = node.Parent;
                        if (node != null && node.NextNode != null)
                        {
                            node = node.NextNode;
                            next = true;
                        }
                    }
                }
            }

            if (node == null) return; // menu not found
            RssMenuNode parentNode = node.Parent;

            do
            {
                if (node.Menu.ID > 0)
                {
                    // insert RSS here
                    this.UpdateNews(node.Menu.ID);
                }

                if (node.Children != null)
                {
                    node = node.Children[0];
                }
                else if (node.NextNode != null)
                {
                    node = node.NextNode;
                }
                else
                {
                    bool next = false;
                    while (!next && node != null)
                    {
                        node = node.Parent;
                        if (node != null && node.NextNode != null)
                        {
                            node = node.NextNode;
                            next = true;
                        }
                    }
                }
            } while (node != null && (node.Menu.ID != menuID && !node.Parent.Equals(parentNode) && !node.Equals(parentNode)));
        }
        catch { }
    }

    public void UpdateNews(int menuID)
    {
        try
        {
            DataTable tb = RssMenuLink.GetAllLinks(menuID, -1, 1);
            if (tb != null)
            {
                foreach (DataRow row in tb.Rows)
                {
                    try
                    {
                        int linkID = (int)row[RssLink.ID__COLUMN_NAME];
                        int siteID = (int)row[RssLink.RSSSITEID__COLUMN_NAME];
                        string rssUrl = row[RssLink.RSSURL__COLUMN_NAME].ToString();
                        
                        RssSite site = RssSite.GetSite(siteID);
                        if (site == null)
                            continue;

                        string rssContent = WebClientUtilities.ReadContent(rssUrl, System.Text.Encoding.UTF8);
                        RssDocument rssDoc = new RssDocument(rssContent, CultureInfo.CreateSpecificCulture(site.Culture), site.DescriptionEncoded);

                        if (rssDoc.Items != null && rssDoc.Items.Count > 0)
                        {
                            foreach (RssItem item in rssDoc.Items)
                            {
                                bool existing = false;
                                if (site.TimeZone >= -12)
                                {
                                    item.Date = item.Date.AddHours(site.TimeZone - 7);
                                }
                                RssContent content = RssContent.TryInsert(linkID, item.Title, item.Description, item.Image, item.Link, item.Date, true, out existing);
                                RssContentInMenu.TryInsert(menuID, content.ID, true, out existing);
                            }
                        }
                    }
                    catch { }
                }
            }
        }
        catch { }
    }
    */
}
